package com.leyou.cart.service;

import com.leyou.cart.pojo.Cart;
import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.auth.pojo.UserInfo;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车业务
 */
@Service
public class CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;


    public void addCart(Cart cart) {
        //1.取出当前登录用户信息
        UserInfo userInfo = UserHolder.getUser();

        //2.获取登录用户userId
        String userId = userInfo.getId().toString();

        //3.根据userId到redis取出hash数据
        BoundHashOperations<String, Object, Object> boundHashOps = redisTemplate.boundHashOps(userId);

        //4.判断当前商品是否已经存在
        //取出skuId
        String skuId = cart.getSkuId().toString();

        if(boundHashOps!=null) {
            if(boundHashOps.hasKey(skuId)){
                //4.1 如果存在，则商品的数量增加
                //1）取出Cart对象
                Object cartJson = boundHashOps.get(skuId);
                //2）转换为Java对象
                Cart tempCart = JsonUtils.toBean(cartJson.toString(),Cart.class);
                //3）修改数量
                cart.setNum(tempCart.getNum() + cart.getNum());
            }
        }
        cart.setUserId(userInfo.getId());
        //往hash数据存入当前商品数据
        boundHashOps.put(skuId,JsonUtils.toString(cart));
    }

    public List<Cart> findCarts() {
        //1.取出当前登录用户信息
        UserInfo userInfo = UserHolder.getUser();

        //2.获取登录用户userId
        String userId = userInfo.getId().toString();

        //3.先取出当前用户购物车数据
        BoundHashOperations<String, Object, Object> boundHashOps = redisTemplate.boundHashOps(userId);

        //4.把购物车数据以List<Cart>结构返回
        List<Cart> carts = null;
        if(boundHashOps!=null){
            carts =  boundHashOps.values()
                    .stream()
                    .map(cartJson->JsonUtils.toBean(cartJson.toString(),Cart.class))
                    .collect(Collectors.toList());
        }
        return carts;
    }

    public void updateNum(Long skuId, Integer num) {
        //1.取出当前登录用户信息
        UserInfo userInfo = UserHolder.getUser();

        //2.获取登录用户userId
        String userId = userInfo.getId().toString();

        //3.先取出当前用户购物车数据
        BoundHashOperations<String, Object, Object> boundHashOps = redisTemplate.boundHashOps(userId);

        //4.取出当前修改数量的商品
        String skuIdStr = skuId.toString();
        if(boundHashOps!=null){
            Object cartJson = boundHashOps.get(skuIdStr);

            //5.转换为Java对象
            Cart cart = JsonUtils.toBean(cartJson.toString(), Cart.class);

            //6.修改数量
            cart.setNum(num);

            //7.更新绑定的对象
            boundHashOps.put(skuIdStr,JsonUtils.toString(cart));
        }

    }

    public void deleteCart(Long skuId) {
        //1.取出当前登录用户信息
        UserInfo userInfo = UserHolder.getUser();

        //2.获取登录用户userId
        String userId = userInfo.getId().toString();

        //3.先取出当前用户购物车数据
        BoundHashOperations<String, Object, Object> boundHashOps = redisTemplate.boundHashOps(userId);

        if(boundHashOps!=null){
            boundHashOps.delete(skuId.toString());
        }
    }

    public void mergeCarts(List<Cart> carts) {
        if(carts!=null){
            carts.forEach(cart -> {
                this.addCart(cart);
            });
        }
    }
}
